
#define NUM_CHOICES		2

#include "LoseGameState.h"

#include "../SGD Wrappers/SGD_AudioManager.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_String.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "Game.h"
#include "BitmapFont.h"
#include "MainMenuState.h"
#include "GameplayState.h"


/**************************************************************/
// GetInstance
/*static*/ LoseGameState* LoseGameState::GetInstance( void )
{
	static LoseGameState s_Instance;

	return &s_Instance;
}


/**************************************************************/
// Enter
/*virtual*/ void LoseGameState::Enter( void )
{
	// Reset the cursor to the top
	// (commented out to keep the last cursor position)
	//m_nCursor = 0;


	// Set background color
	SGD::GraphicsManager::GetInstance()->SetClearColor( {0, 0, 0} );	// black


	// Load assets
}

/**************************************************************/
// Exit
/*virtual*/ void LoseGameState::Exit( void )
{
	// Unload assets


}


/**************************************************************/
// Input
/*virtual*/ bool LoseGameState::Input( void )
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();

	// Press Escape to quit
	//if( pInput->IsKeyPressed( SGD::Key::Escape ) == true )
	//	Game::GetInstance()->ChangeState(CreditsState::GetInstance());
		//return false;	// quit game


	if (pInput->IsKeyPressed(SGD::Key::Down) == true)
		m_nCursor = m_nCursor + 1 < NUM_CHOICES ? m_nCursor + 1 : 0;
	else if (pInput->IsKeyPressed(SGD::Key::Up) == true)
		m_nCursor = m_nCursor - 1 >= 0 ? m_nCursor - 1 : NUM_CHOICES - 1;

	if (pInput->IsKeyPressed(SGD::Key::Enter) == true)
	{

		switch (m_nCursor)
		{
		case 0: // gameplay
			{
					SGD::Event msg("UNPAUSE");
					msg.SendEventNow();
					//Game::GetInstance()->RemoveState();
					Game::GetInstance()->RemoveState();
					Game::GetInstance()->RemoveState();
					Game::GetInstance()->AddState(GameplayState::GetInstance());

				return true;
			}
			break;

		case 1: // main menu
			{
				Game::GetInstance()->RemoveState();
				Game::GetInstance()->RemoveState();
				Game::GetInstance()->AddState(MainMenuState::GetInstance());
				return true;
			}
			break;
		}
	}


	return true;	// keep playing
}


/**************************************************************/
// Update
/*virtual*/ void LoseGameState::Update( float elapsedTime )
{
	/*
	if (m_pLoseAnimation != nullptr && m_pLoseAnimation->IsPlaying() == true)
	{
		m_pLoseAnimation->Update(elapsedTime);
	}
	*/
}


/**************************************************************/
// Render
/*virtual*/ void LoseGameState::Render( void )
{
	// Use the game's font
	const BitmapFont* pFont = Game::GetInstance()->GetFont();

	// Align text based on window width
	float width		= Game::GetInstance()->GetScreenWidth();
	float height	= Game::GetInstance()->GetScreenHeight();
	float scale		= 0.9f;
	float left_start = 50.0F;											// 50


	// Display the game title centered at 4x scale
	pFont->Draw( "GAME OVER!", { (width - (10 * 32 * 3.0f))/2, height * 0.25F - (26.0F * 3.0F) }, 3.0f, { 255, 255, 255 } );



	//position.x	= (width * 0.5F) - 30.0F * scale;
	//position.y	= (height * 0.5F) - 30.0F * scale;

	/*
	if (m_pLoseAnimation != nullptr && m_pLoseAnimation->IsPlaying() == true)
		m_pLoseAnimation->Render(position, scale, { 255, 255, 255, 255 });
	*/


	pFont->Draw("Continue?", { (width - (9 * 32 * scale)) / 2, height * 0.5F }, scale, { 255, 255, 0 });

	pFont->Draw("YES", { (width - (3 * 32 * scale)) / 2, (height * 0.5F) + 100.0f }, scale, { 0, 255, 0 });
	pFont->Draw("NO", { (width - (2 * 32 * scale)) / 2, (height * 0.5F) + 200.0f }, scale, { 0, 255, 0 });




	const char* output = "";
	int length = 0;

	if (m_nCursor == 0)
	{
		output = "=    =";
		length = 3;
	}
	else if (m_nCursor == 1)
	{
		output = "=   =";
		length = 2;
	}


	pFont->Draw(output, { (width - (length * 32)) / 2 - 32.0F, (height * 0.5F) + (100.0f * m_nCursor) + 100.0f }, 1.0f, { 255, 0, 0 });
	//pFont->Draw(output, position, 1.0f, { 255, 0, 0 });

}
